<?php
namespace Admin\Controller;

use Think\Controller\RestController;
class IndexController extends RestController{

    public function index(){
		$classes=M("course_list")->where("pid=0")->order("paixu")->select();
		$this->assign("classes",$classes);
	

        $this->display('Index/index');
    }




    public function login()
    {
    	$this->display('Index/login');
    }





    public function loginup()
    {
    //   switch ($this->_method){
    //     case 'post': // get请求处理代码
    //       if ($this->_type == 'html'){
    //         // dump($_POST);
    //         // exit;
         
    //         $list = M('user')->where(array('user'=>$_POST['name']))->select();
    //         // dump($list);
    //         // dump($_POST);
    //         // exit;
    //         if ($_POST['name'] == $list[0]['user'] && $_POST['pwd'] == $list[0]['password']) {
    //           $data = array(
    //             "code"=>1,
    //             "message"=>"登入成功",
    //             "data" =>array(
    //               "id"=>$list[0]['id'],
    //               "name"=>$list[0]['user'],
    //               "password"=>md5($list[0]['pwd']),
    //               "tel" => $list[0]['tel'],
    //               "email"=>$list[0]['email'],
    //               "sex"=>$list[0]['sex'],
    //               "login_time"=>date('Y-m-d H:i:s')
    //               )
    //             );
              
    //           $this->response($data,'json');
    //         }else{
    //             $data = array(
    //               "code"=>0,
    //               "message"=>"登入失败",
    //               "data"=>"{}"
    //             );
    //           $this->response($data,'json');
    //           // echo json_encode($data);
    //         }


    //       }
    //       break;
    //     case 'put': // put请求处理代码
    //       break;
    //     case 'get': // post请求处理代码
    //       break;
      
    // } 
    	 // dump($_POST);
      //  exit;
      $user = $_POST['name'];
      $pwd  = $_POST['pwd'];

      $list = M('auser')->where(array('name'=>$user))->select();
      // dump($list);
      // exit;
      // if (empty($list)) {
      //   $arr = array(
      //     "code"=>0,
      //     "message"=>"登入失败",
      //     "data"=>"{}"
      //     );
      //   echo json_encode($arr);
      //   // dump($arr);
          
      // }

       if (IS_POST) {
       		if (empty($_POST)) {
       			$this->error('您还什么都没有填');
       			exit;
       		}
       		$name = $_POST['name'];
       		$pwd = $_POST['pwd'];

       		$list = M('auser')->where(array('name'=>$name))->select();
       		// dump($list);
       		// exit;
       		$user = $list[0]['name'];
       		$pwdd = $list[0]['password'];
       		$id = $list[0]['id'];
       		 if ($name == $user && $pwd == $pwdd) {
                 $_SESSION['admin']['user'] = $name;
                 $_SESSION['admin']['id'] = $id;

        $list = M('power')->field('cname,mname')->where('id in'.M('role_power')->field('p_id')->where("r_id in ".M('user_role')->field('r_id')->where(array('u_id'=>array('eq',$id)))->buildSql())->buildSql())->select();

        //控制器名转换为大写
        foreach ($list as $key => $val) {
            $list[$key]['cname'] = ucfirst($val['cname']);
        }

        // //查看查询出来的信息
        // echo '<pre>';
        //     print_r($list); 
        // echo '</pre>';
        // exit;
        $nodelist = array();
        $nodelist['Index'] = array('index');
        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['cname']][] = $v['mname'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['mname']=="edit"){
                $nodelist[$v['cname']][]="save";
            }
            if($v['mname']=="add"){
                $nodelist[$v['cname']][]="doadd";
            }
        }

        //将权限信息放置到session中
        $_SESSION['admin']['nodelist'] = $nodelist;

        //重组的信息
        // V($_SESSION);exit;
        
      

                $this->success('登入成功',U('Index/index'));

            }else{
                $this->error('登入失败');

               }


       }else{
       	$this->error('请求失败!!!');
       }
    }




    public function logout(){
    	unset($_SESSION['home']);

    	$this->success('成功退出!!',U('Home/Index/index'));
    }






}